package com.yanchangapp.widget;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.yanchangapp.R;
import com.yanchangapp.adapter.SupplierAdapter;
import com.yanchangapp.model.bean.SupplierBean;
import com.zxl.baselib.util.ui.ResHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 2019-09-12.
 * description:供应商列表BottomDialog的代理类
 */
public class SupplierListBottomDelegate {

    public interface SupplierDismissListener{
        void  onDismiss();
    }

    public interface OnEnsureClickListener{
        void onEnsure(String supplierID,String supplierName);
    }

    public static class Builder{
        private Context mContext;
        private List<SupplierBean> mSupplierBeans,innerList;
        private SupplierDismissListener mSupplierDismissListener;
        private OnEnsureClickListener mOnEnsureClickListener;
        private BottomDialog mSupplierListDialog;
        private SupplierAdapter mMSupplierAdapter;
        private String selectSupplierId;


        public Builder(Context context) {
            mContext = context;
        }

        public Builder setSelectSupplier(String selectSupplierId){
            this.selectSupplierId = selectSupplierId;
            return this;
        }

        public Builder setData(List<SupplierBean> supplierBeans){
            this.mSupplierBeans = supplierBeans;
            return this;
        }

        public Builder setDismissListener(SupplierDismissListener dismissListener){
            this.mSupplierDismissListener = dismissListener;
            return this;
        }

        public Builder setEnsureListener(OnEnsureClickListener ensureListener){
            this.mOnEnsureClickListener = ensureListener;
            return this;
        }

        public Builder create(){
            innerList = new ArrayList<>();
            mSupplierListDialog = new BottomDialog(mContext, 0, true);
            View view = LayoutInflater.from(mContext).inflate(R.layout.bottom_goods_supplier, null, false);
            mSupplierListDialog.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mSupplierListDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(ResHelper.getColor(android.R.color.transparent));
            view.findViewById(R.id.closeBottom).setOnClickListener(v -> mSupplierListDialog.dismiss());
            RecyclerView mSupplierRv = view.findViewById(R.id.rvSupplier);
            mSupplierRv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            mSupplierRv.addItemDecoration(new RecycleViewDivider(
                    mContext, LinearLayoutManager.HORIZONTAL,
                    ScreenUtils.dip2px(mContext, 0.5f),
                    ResHelper.getColor(R.color.borderColorBase)));
            mMSupplierAdapter = new SupplierAdapter(R.layout.item_checkbox_text, innerList);
            mMSupplierAdapter.setOnItemClickListener((adapter, view1, position) -> {
                if (null != mOnEnsureClickListener){
                    mOnEnsureClickListener.onEnsure(mSupplierBeans.get(position).getSupplierId(),
                            mSupplierBeans.get(position).getSupplierName());
                }
                mSupplierListDialog.dismiss();
            });
            mSupplierRv.setAdapter(mMSupplierAdapter);
            mSupplierListDialog.setOnDismissListener(dialog -> {
                if (null != mSupplierDismissListener){
                    mSupplierDismissListener.onDismiss();
                }
            });
            return this;
        }

        public void show(){
            for (int i = 0; i < mSupplierBeans.size(); i++) {
                SupplierBean bean = mSupplierBeans.get(i);
                // 选中设置为true
                if (bean.getSupplierId().equals(selectSupplierId)) {
                    bean.setChecked(true);
                } else {
                    bean.setChecked(false);
                }
            }
            innerList.clear();
            innerList.addAll(mSupplierBeans);
            mMSupplierAdapter.notifyDataSetChanged();
            if (mSupplierListDialog != null){
                mSupplierListDialog.show();
            }
        }

        public void dismiss(){
            if (mSupplierListDialog != null){
                mSupplierListDialog.dismiss();
            }
        }

    }
}
